package com.westcom.healthmate.common.exception;

import com.westcom.healthmate.common.constants.ErrorConstants;
import org.springframework.util.StringUtils;

public class ErrorException extends Exception
{
    private String code = null;
    private int httpStatus;
    private String message = null;

    public ErrorException(String code)
    {
        setCode(code);
    }
    public ErrorException (String msg,String code)
    {
        super(msg);
        this.message = msg;
        setCode(code);
    }
    public ErrorException(Exception e)
    {
        super(e);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code)
    {
        ErrorConstants.OperationStatus status = ErrorConstants.OPERATION_STATUS.get(code);
        if(null != status)
        {
            this.httpStatus = status.getHttpStatus();
            this.message = status.getMsg();
        }
        this.code = code;
    }

    public int getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
    }

    @Override
    public String getMessage()
    {
        if(StringUtils.isEmpty(this.message))
        {
            return super.getMessage();
        }
        return this.message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}
